package cn.wh.mybatis.session;

import cn.wh.mybatis.binding.MapperRegistry;
import cn.wh.mybatis.datasource.druid.DruidDataSourceFactory;
import cn.wh.mybatis.datasource.pooled.PooledDataSourceFactory;
import cn.wh.mybatis.datasource.unpooled.UnpooledDataSourceFactory;
import cn.wh.mybatis.mapping.Environment;
import cn.wh.mybatis.mapping.MappedStatement;
import cn.wh.mybatis.transaction.jdbc.JdbcTransactionFactory;
import cn.wh.mybatis.type.TypeAliasRegistry;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 配置项
 * @Version 1.0.0
 * @Date 2022/12/8 15:46
 * @Created wh
 */
public class Configuration {
    //环境
    protected Environment environment;

    //映射注册机
    protected MapperRegistry mapperRegistry = new MapperRegistry(this);

    //映射的语句 存在Map里
    protected final Map<String, MappedStatement>mappedStatements=new HashMap<>();

    //类型别名注册机
    protected final TypeAliasRegistry typeAliasRegistry=new TypeAliasRegistry();


    public Configuration(){
        typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);

        typeAliasRegistry.registerAlias("DRUID", DruidDataSourceFactory.class);
        typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);
        typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
    }

    public void addMappers(String packageName){
            mapperRegistry.addMappers(packageName);
    }


    public <T> void addMapper(Class<T> type){
           mapperRegistry.addMapper(type);
    }

    public<T> T getMapper(Class<T> type,SqlSession sqlSession){
        return mapperRegistry.getMapper(type,sqlSession);
    }

    public boolean hasMapper(Class<?> type){
        return mapperRegistry.hasMapper(type);
    }

    public void addMappedStatement(MappedStatement ms){
        mappedStatements.put(ms.getId(),ms);
    }


    public MappedStatement getMappedStatement(String id){
        return mappedStatements.get(id);
    }

    public TypeAliasRegistry getTypeAliasRegistry(){
        return typeAliasRegistry;
    }



    public Environment getEnvironment() {
        return environment;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

}
